package view.swing.tiles;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

public class TileTransferable implements Transferable {

    public static DataFlavor DATA_FLAVOR;
    private TileDragDroppable tile;

    private TileTransferable() {
        String mimeType = DataFlavor.javaJVMLocalObjectMimeType + ";class="
                + TileDragDroppable.class.getName();
        try {
            DATA_FLAVOR = new DataFlavor(mimeType);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    public TileTransferable(TileDragDroppable c) {
        this();
        this.tile = c;
    }

    @Override
    public Object getTransferData(DataFlavor flavor)
            throws UnsupportedFlavorException, IOException {
        if (flavor == null) throw new IOException();
        if (flavor.equals(DATA_FLAVOR)) return this.tile;
        else throw new UnsupportedFlavorException(flavor);
    }

    @Override
    public DataFlavor[] getTransferDataFlavors() {
        return new DataFlavor[] { DATA_FLAVOR };
    }

    @Override
    public boolean isDataFlavorSupported(DataFlavor flavor) {
        return flavor.equals(DATA_FLAVOR);
    }

}
